package com.yunke.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunke.common.result.YunkeResult;
import com.yunke.service.YunkeDataStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据统计Controller
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
@Validated
@Tag(name = "数据统计", description = "数据统计分析相关接口")
public class YunkeDataStatisticsController {

    private final YunkeDataStatisticsService statisticsService;

    @GetMapping("/dashboard")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取仪表板概览数据", description = "获取平台核心数据概览")
    public YunkeResult<Map<String, Object>> getDashboardOverview() {
        return statisticsService.getDashboardOverview();
    }

    @GetMapping("/user")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取用户统计数据", description = "获取用户注册、活跃等统计数据")
    public YunkeResult<Map<String, Object>> getUserStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate,
            @Parameter(description = "统计维度") @RequestParam(defaultValue = "1") Integer dimension) {
        return statisticsService.getUserStatistics(startDate, endDate, dimension);
    }

    @GetMapping("/partner")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取搭子统计数据", description = "获取搭子发布、参与等统计数据")
    public YunkeResult<Map<String, Object>> getPartnerStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate,
            @Parameter(description = "统计维度") @RequestParam(defaultValue = "1") Integer dimension) {
        return statisticsService.getPartnerStatistics(startDate, endDate, dimension);
    }

    @GetMapping("/service")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取服务统计数据", description = "获取服务发布、交易等统计数据")
    public YunkeResult<Map<String, Object>> getServiceStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate,
            @Parameter(description = "统计维度") @RequestParam(defaultValue = "1") Integer dimension) {
        return statisticsService.getServiceStatistics(startDate, endDate, dimension);
    }

    @GetMapping("/order")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取订单统计数据", description = "获取订单数量、金额等统计数据")
    public YunkeResult<Map<String, Object>> getOrderStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate,
            @Parameter(description = "统计维度") @RequestParam(defaultValue = "1") Integer dimension) {
        return statisticsService.getOrderStatistics(startDate, endDate, dimension);
    }

    @GetMapping("/finance")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取财务统计数据", description = "获取收入、支出、利润等统计数据")
    public YunkeResult<Map<String, Object>> getFinanceStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate,
            @Parameter(description = "统计维度") @RequestParam(defaultValue = "1") Integer dimension) {
        return statisticsService.getFinanceStatistics(startDate, endDate, dimension);
    }

    @GetMapping("/distribution")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取分销统计数据", description = "获取分销商、佣金等统计数据")
    public YunkeResult<Map<String, Object>> getDistributionStatistics(
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate,
            @Parameter(description = "统计维度") @RequestParam(defaultValue = "1") Integer dimension) {
        return statisticsService.getDistributionStatistics(startDate, endDate, dimension);
    }

    @GetMapping("/realtime")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取实时数据", description = "获取平台实时运营数据")
    public YunkeResult<Map<String, Object>> getRealTimeData() {
        return statisticsService.getRealTimeData();
    }

    @GetMapping("/trend")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取趋势分析数据", description = "获取数据变化趋势分析")
    public YunkeResult<List<Map<String, Object>>> getTrendAnalysis(
            @Parameter(description = "统计类型") @RequestParam Integer type,
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate) {
        return statisticsService.getTrendAnalysis(type, startDate, endDate);
    }

    @GetMapping("/ranking")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取热门数据排行", description = "获取各类热门数据排行榜")
    public YunkeResult<Map<String, Object>> getHotRanking() {
        return statisticsService.getHotRanking();
    }

    @GetMapping("/region")
    @SaCheckPermission("statistics:view")
    @Operation(summary = "获取地区分析数据", description = "获取各地区用户分布和活跃度")
    public YunkeResult<List<Map<String, Object>>> getRegionAnalysis() {
        return statisticsService.getRegionAnalysis();
    }

    @GetMapping("/export")
    @SaCheckPermission("statistics:export")
    @Operation(summary = "导出统计报表", description = "导出指定类型的统计报表")
    public YunkeResult<String> exportReport(
            @Parameter(description = "报表类型") @RequestParam Integer type,
            @Parameter(description = "开始日期") @RequestParam String startDate,
            @Parameter(description = "结束日期") @RequestParam String endDate) {
        return statisticsService.exportReport(type, startDate, endDate);
    }

    @PostMapping("/generate")
    @SaCheckPermission("statistics:manage")
    @Operation(summary = "生成统计数据", description = "手动生成指定日期的统计数据")
    public YunkeResult<Void> generateStatistics(
            @Parameter(description = "统计日期") @RequestParam String date) {
        return statisticsService.generateStatistics(date);
    }

    @DeleteMapping("/clean")
    @SaCheckPermission("statistics:manage")
    @Operation(summary = "清理过期统计数据", description = "清理指定天数之前的统计数据")
    public YunkeResult<Void> cleanExpiredData(
            @Parameter(description = "保留天数") @RequestParam Integer days) {
        return statisticsService.cleanExpiredData(days);
    }
} 